<?php include('include/entete.html'); ?>


<br/><br/>
 
<div class="contenu">
	<table align="center" width="100%" cellpadding="3" cellspacing="0">
	<tr>
		<td id="cell1">Recherche emplacement</td>
	</tr>
	<tr>
		<td id="cell2">

<a href="index.php">Liste Pays</a>

<?php
	
	try {
		$db = new PDO('mysql:host=localhost;dbname=campingcar', 'root', '');
		//echo 'Connected to database';
    }
	catch(PDOException $e) {
		echo $e->getMessage();
    }
	
	if (isset($_GET['position'])) {
		$position = trim($_GET['position']);
		//echo $position."<br/>";
		if (strpos($position,'E')) {
			$lat = trim(substr($position, 1, strpos($position,'E')-1));
		} else {
			$lat = trim(substr($position, 1, strpos($position,'W')-1));
		}
		//echo $lat."<br/>";
		if (strpos($position,'E')) {
			$long = trim(substr($position, strpos($position,'E')+1));
		} else {
			$long = trim(substr($position, strpos($position,'W')+1));
		}
	//echo $long."<br/>";
	} else {
		$position = 'N45.54798 E3.73525'; 
    }
	
	if (isset($_GET['rayon'])) {
		$rayon = trim($_GET['rayon']);
		//echo $rayon."<br/>";
	} else {
		$rayon = 30;
	}
?>

<div class="table">
<form action="form_recherche_emplacement.php" name="rechercherEmplacement" method="get">
<table border="0" width="100%">
	<tr>
		<th class="thead">Position actuelle</th>
		<td class="ligne2"><input type="text" name="position" value="<?php echo $position; ?>"></td>
	</tr>
	<tr>
		<th class="thead">Rayon</th>
		<td class="ligne2"><input type="text" name="rayon" value="<?php echo $rayon; ?>" size="3">Km</td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" name="Rechercher"></td>
	</tr>
</table>
</form>
</div>

</td>
</tr>
</table>
</div>

<br/>

<div class="contenu100">
	<table align="center" width="100%" cellpadding="3" cellspacing="0">
	<tr>
		<td id="cell1">Resultats</td>
	</tr>
	<tr>
		<td id="cell2">
<?php
 
	if (isset($lat) && isset($long)) {
		if (strpos($position,'E')) {
			$query = "select * from (SELECT id, pays, num, type_emplacement_id, localite, GPSN, GPSE, adresse, tarifs, type_de_borne ,eau_potable ,vidange_eaux_usees ,vidange_wc ,v220 ,wc_publics ,autres_services ,autres_infos ,creation_date ,last_update_date ,".
					"convert(pow(pow(abs(".$lat."-convert(GPSN, decimal (8, 5)))*nb_km_par_lat , 2)+pow(abs(".$long."-convert(GPSE, decimal (8, 5)))*nb_km_par_long ,2),0.5),decimal (5, 2)) distance ".
					"FROM emplacement e, options WHERE gpse NOT LIKE '-%' ORDER BY distance LIMIT 0 , 30 ) t WHERE distance <= ".$rayon;
		} else {
			$query = "select * from (SELECT id, pays, num, type_emplacement_id, localite, GPSN, GPSE, adresse, tarifs, type_de_borne ,eau_potable ,vidange_eaux_usees ,vidange_wc ,v220 ,wc_publics ,autres_services ,autres_infos ,creation_date ,last_update_date ,".
					"convert(pow(pow(abs(".$lat."-convert(GPSN, decimal (8, 5)))*nb_km_par_lat , 2)+pow(abs(".$long."-abs(convert(GPSE, decimal (8, 5))))*nb_km_par_long ,2),0.5),decimal (5, 2)) distance ".
					"FROM emplacement e, options WHERE gpse LIKE '-%' ORDER BY distance LIMIT 0 , 30 ) t WHERE distance <= ".$rayon;
		}
		//echo "QRY : ".$query."<br/>";
		
		$select_messages = $db->prepare($query);
		$select_messages->execute();
	}

	
	if (isset($select_messages)) {
	
		echo '<div class="table100">';
		echo '<table border="0" width="100%">';
		echo '	<tr class="thead">';
		echo '		<th>Id</th>';
		echo '		<th>Type</th>';
		echo '		<th>Localite</th>';
		echo '		<th>Distance (km)</th>';
		echo '		<th>Tarifs</th>';
		echo '		<th>WC publics</th>';
		echo '		<th>Autres services</th>';
		echo '		<th>Autres infos</th>';
		echo '		<th>Coordonnees (MapSource)</th>';
		echo '	</tr>';

		$couleur = array(0 => "ligne1", 1 => "ligne2");
		$i = 0;
		foreach($select_messages->fetchAll() as $rec) {
			echo '<tr class="'.$couleur[$i%2].'">';
			echo '<td>'.$rec['id'].'</td>';
			echo '<td>'.$rec['type_emplacement_id'].'</td>';
			echo '<td><a href="emplacement.php?pays='.$rec['pays'].'&id='.$rec['id'].'">'.$rec['localite'].'</a></td>';
			echo '<td>'.$rec['distance'].'</td>';
			echo '<td>'.$rec['tarifs'].'</td>';
			echo '<td>'.$rec['wc_publics'].'</td>';
			echo '<td>'.$rec['autres_services'].'</td>';
			echo '<td>'.$rec['autres_infos'].'</td>';
			if (doubleval($rec['GPSE'])<0) {
				echo '<td>N'.doubleval($rec['GPSN']).' W'.abs(doubleval($rec['GPSE'])).'</td>';
			} else {
				echo '<td>N'.doubleval($rec['GPSN']).' E'.doubleval($rec['GPSE']).'</td>';
			}
			echo '</tr>';
			$i++;
		}
		
		echo '</table>';
		echo '</div>';
	}
?>


</td>
</tr>
</table>
</div>

<br/><br/>
 
<?php include('include/piedPage.html'); ?>
            